#include <string.h>
#include "mtl_com.h"
#include "R_SPI_FLASH_m25p.h"
#include "R_SPI_FLASH_m25p_io.h"


/************************************************************************************************/
/*	Definitions																					*/
/************************************************************************************************/

#define FLASH_TEST_BUF_SIZE			512


/************************************************************************************************/
/*	Unions																						*/
/************************************************************************************************/


/************************************************************************************************/
/*	Structs																						*/
/************************************************************************************************/


/************************************************************************************************/
/*	Globals																						*/
/************************************************************************************************/
uchar FAR				cbuf1[FLASH_TEST_BUF_SIZE];
uchar FAR				cbuf2[FLASH_TEST_BUF_SIZE];

uchar					gDevNo;
ulong					gAddr;
uchar					gStat;
uchar					gID;
uchar					gID2[3];

/************************************************************************************************/
/*	Externs																						*/
/************************************************************************************************/


/************************************************************************************************/
/*	Macros																						*/
/************************************************************************************************/


/************************************************************************************************/
/*	Prototypes																					*/
/************************************************************************************************/

void Test100(void);
void Test200(void);
void Test300(void);
void Test400(void);
void tst_trap(void);

/*----------------------------------------------------------------------------------------------*/
void tst_trap(void)
{
	
	while(1){}
}
/*----------------------------------------------------------------------------------------------*/
void testmain(void)
{

	gDevNo  = FLASH_DEV0;
	
	/* EEPROM driver initialization */
	R_SPI_FLASH_Init_Driver();
	
	/* Write protect & Read status sample */
	Test100();
	
	/* Erase & Write & Read sample */
	Test200();
	
	/* Deep power down & Release deep power down sample */
	Test300();
	
	/* Read ID sample */
	Test400();
	
	while(1);

}


/*----------------------------------------------------------------------------------------------*/
void Test100(void)	
{
	short				Ret;

/*---------------------------------------------------*/
/*                  Protect bit set                  */
/*---------------------------------------------------*/

	Ret = R_SPI_FLASH_Write_Protect(gDevNo, 1);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

	Ret = R_SPI_FLASH_Read_Status(gDevNo, &gStat);	
	if (Ret < FLASH_OK) {
		tst_trap();
	}

    if((gStat & 0x1C) !=0x04){
		tst_trap();
	}


/*---------------------------------------------------*/
/*               release Protect bit                 */
/*---------------------------------------------------*/
	Ret = R_SPI_FLASH_Write_Protect(gDevNo, 0);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

	Ret = R_SPI_FLASH_Read_Status(gDevNo, &gStat);	
	if (Ret < FLASH_OK) {
		tst_trap();
	}

    if((gStat & 0x1C) !=0x00){
		tst_trap();
	}



}

/*----------------------------------------------------------------------------------------------*/
void Test200(void)
{
	short				Ret;
	ushort				i;

/*---------------------------------------------------*/
/*            Sector erase & write & read            */
/*---------------------------------------------------*/
	gAddr = 0x0123;
	
	Ret = R_SPI_FLASH_SectorErase(gDevNo,gAddr);
	if (Ret < FLASH_OK) {
		tst_trap();
	}
	
	gDevNo  = FLASH_DEV0;
	
	
	for (i = 0; i < 2; i++) {
		*(cbuf1 + i) = i + 1;
	}
	
	Ret = R_SPI_FLASH_Write_Data(gDevNo, gAddr, 2, cbuf1);
	if (Ret < FLASH_OK) {
		tst_trap();
	}
	
	memset(cbuf2, 0xFF, 2);


	/* data read */
	Ret = R_SPI_FLASH_Read_Data(gDevNo, gAddr, 2, cbuf2);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

	/* data compare */
	if (memcmp(cbuf1, cbuf2, 2) != 0) {
		tst_trap();
	}
	
	Ret = R_SPI_FLASH_SectorErase(gDevNo,gAddr);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

	memset(cbuf1, 0xFF, 2);

	/* data read */
	Ret = R_SPI_FLASH_Read_Data(gDevNo, gAddr, 2, cbuf2);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

	/* data compare */
	if (memcmp(cbuf1, cbuf2, 2) != 0) {
		tst_trap();
	}


/*---------------------------------------------------*/
/*             Bulk erase & write & read             */
/*---------------------------------------------------*/
	gAddr = 0x0123;	
	gDevNo  = FLASH_DEV0;
	
	
	for (i = 0; i < 2; i++) {
		*(cbuf1 + i) = i + 1;
	}
	
	Ret = R_SPI_FLASH_Write_Data(gDevNo, gAddr, 2, cbuf1);
	if (Ret < FLASH_OK) {
		tst_trap();
	}
	
	memset(cbuf2, 0xFF, 2);


	/* data read */
	Ret = R_SPI_FLASH_Read_Data(gDevNo, gAddr, 2, cbuf2);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

	/* data compare */
	if (memcmp(cbuf1, cbuf2, 2) != 0) {
		tst_trap();
	}
	
	Ret = R_SPI_FLASH_BulkErase(gDevNo);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

	memset(cbuf1, 0xFF, 2);

	/* data read */
	Ret = R_SPI_FLASH_Read_Data(gDevNo, gAddr, 2, cbuf2);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

	/* data compare */
	if (memcmp(cbuf1, cbuf2, 2) != 0) {
		tst_trap();
	}

}
/*----------------------------------------------------------------------------------------------*/

void Test300(void)	
{
	short				Ret;
	
/*---------------------------------------------------*/
/*	                Deep power down                  */
/*---------------------------------------------------*/

	/* Deep power down */
	Ret = R_SPI_FLASH_DeepPDown(gDevNo);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

/*---------------------------------------------------*/
/*               Release deep power down             */
/*---------------------------------------------------*/

	/* Release deep power down*/
	Ret = R_SPI_FLASH_ReleaseDeepPDown(gDevNo,&gID);
	if (Ret < FLASH_OK) {
		tst_trap();
	}
	
}

/*----------------------------------------------------------------------------------------------*/

void Test400(void)	
{
	short				Ret;

/*---------------------------------------------------*/
/*                      Read ID                      */
/*---------------------------------------------------*/

	Ret = R_SPI_FLASH_ReadID(gDevNo, gID2);
	if (Ret < FLASH_OK) {
		tst_trap();
	}

}


